<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 
        打印出1000以内所有的"水仙花数"
        水仙花数是指一个 3 位数，它的每个位上的数字的 3次幂之和等于它本身。
     -->
</head>

<body>
    <div id="dv">
        <p>打印出1000以内所有的"水仙花数" <br>
            水仙花数是指一个 3 位数，它的每个位上的数字的 3次幂之和等于它本身。</p>
    </div>
</body>
<script>
    console.log(isNarcissisticNumber(153));

    for (let i = 100; i < 1000; i++) {
        // console.log(isNarcissisticNumber(i));
        if (isNarcissisticNumber(i)) {
            let pObj = document.createElement("p");
            pObj.innerText = i;
            document.getElementById("dv").appendChild(pObj);
        }
    }

    function isNarcissisticNumber(num) {
        const number = num; //定义常量用于下面判断
        let a = []; //获取到num的每一位上的数字
        let i = 0;
        while (num > 9) {
            a[i] = num % 10;
            num = parseInt(num / 10);
            i++;
        }
        a[i] = num;
        //console.log(a); //测试
        let n = 0,
            flag = false;
        for (let j = 0; j < a.length; j++) {
            n += Math.pow(a[j], 3);
        }
        // console.log('===', n, number);
        if (n == number) {
            flag = true;
        }
        return flag;
    }
</script>

</html>